﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using Dict.Blo;
using Dict.DataModel;

namespace Login
{
    public class ChoseRole : MonoBehaviour
    {
        public UIInput inputBox;
        public UITexture boyTexture;
        public UITexture girlTexture;
        public LoginManager manager;


        int gender = 1;
        public const string boyHeroId = "hero_043";
        public const string girlHeroId = "hero_047";
        public const string equipId = "equip_037";
        string curHeroId;

        public enum MainActorSex
        {
            Male,
            Female,
        }

        //拖拽转动
        GameObject m_heroAni;
        Vector3 startDragPos;
        bool isDragging;

        public ChoseRoleModel choseRoleModel;


        void Start()
        {
            gender = Random.Range(0,2);
            if (gender == 1)
                OnBoyClick();
            else if (gender == 0)
                OnGirlClick();
            else
            {
                gender = 1;
                OnBoyClick();
            }

            OnRandomBtn();
            if (GuideManager.Instance != null)
            {
                GuideManager.Instance.OnChooseRole();
            }

            //SetGlow11();
            //Debug.Log("SetGlow11()");
        }

        public void OnRandomBtn()
        {
            CSGenerateName msg = new CSGenerateName();
            msg.gender = gender;
            NetAPI.SendHttp(OpDefine.CSGenerateName, msg, GenerateNameCallBack);
        }

        void GenerateNameCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCGenerateName msg = packet.kBody as SCGenerateName;
                inputBox.value = msg.playerName;
            }
        }

        bool createRoleLocked = false;

        public void OnCreateRoleBtn()
        {
            if (createRoleLocked)
            {
                return;
            }
            createRoleLocked = true;

            int count = 0;
            char[] temp = inputBox.value.ToCharArray();
            foreach (var c in temp)
            {
                if (c <= 256)
                    count += 1;
                else
                    count += 2;
            }
            if (count > 12)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("name_too_long_tip"));
                return;
            }

            CSPlayerCreate msg = new CSPlayerCreate();
            msg.playerName = inputBox.value;
            msg.heroId = curHeroId;
            msg.gender = gender;
            NetAPI.SendHttp(OpDefine.CSPlayerCreate, msg, PlayerCreateCallBack);
        }

        void PlayerCreateCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCPlayerCreate msg = packet.kBody as SCPlayerCreate;
                if (msg != null && manager != null)
                {
                    manager.SwitchState(LoginState.PlayerLogin);
					#if !NAKED
                    if(GameConfig.Instance.platform != 1)
                    {
                        PlatformSDK.GetInstance().CreateRoleSubmitData();
                    }
					#endif
                }

                //choseRoleModel.Unload();
            }
            else
            {
                Debug.Log("Create Role Fail CSPlayerCreate");
                //BoxManager.CreatePopupTextBox(LanguageTool.GetTextNoUpdate(1047)+"error code ["+OpDefine.CSPlayerCreate+"]");
                createRoleLocked = false;
            }
        }

        IEnumerator LoadModule(GameObject model, Dict.DataModel.TableHero dh)
        {
            yield return StartCoroutine(ResourceLoader.GetInstance().modelLoader.LoadModel(dh.resName, (asset) =>
            {
                GameObject heroAni = asset as GameObject;
                if (heroAni != null)
                {
                    heroAni.transform.SetParent(transform);
                    heroAni.transform.localRotation = new Quaternion(0, 180, 0, 0);
                    heroAni.transform.localPosition = new Vector3(0, 0, -300);
                    heroAni.transform.localScale = new Vector3(350, 350, 350);
                    model = heroAni;
                }
            }));
            if (model != null)
            {
                model.GetComponent<ModelRenderKit>().SwitchLod(false);
                yield return StartCoroutine(model.GetComponent<ModelRenderKit>().LoadWeapons(dh.id, equipId));
            }
        }

        public void OnBoyClick()
        {
            float a = (float)60 / 255;
            boyTexture.color = new Color(1, 1, 1, 1);
            girlTexture.color = new Color(a, a, a, 1);

            choseRoleModel.OnChoseRole(MainActorSex.Male);

            gender = 1;
            curHeroId = boyHeroId;
        }

        public void OnGirlClick()
        {
            float a = (float)60 / 255;
            girlTexture.color = new Color(1, 1, 1, 1);
            boyTexture.color = new Color(a, a, a, 1);

            choseRoleModel.OnChoseRole(MainActorSex.Female);

            gender = 0;
            curHeroId = girlHeroId;
        }


        IEnumerator PlayStand(GameObject module)
        {
            float length = module.GetComponent<LegacyAnimation>().GetAniLength(AniName.WIN);
            yield return new WaitForSeconds(length);

            module.GetComponent<LegacyAnimation>().anim.CrossFadeQueued(AniName.STAND, 0.3f, QueueMode.PlayNow);
            module.GetComponent<LegacyAnimation>().anim.wrapMode = WrapMode.Loop;
        }

        void SetGlow11()
        {
            //RenderManager.Quality = RenderLevel.High;
            var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
            //if (scene != null)
            //{
                var sceneName = scene.name;
                var str = sceneName + "/Main Camera";
                var obj = GameObject.Find(str);
                if (obj != null)
                {
                    var glow11 = obj.GetComponent<Glow11.Glow11>();
                    if (glow11 != null)
                    {
                        glow11.enabled = true;
                    }
                }

            //}
        }
    }

}